package org.dei.perla.sys.registry.datastructure.dataStructureTestUnit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.dei.perla.sys.registry.datastructure.BTree;
import org.dei.perla.sys.registry.datastructure.BTreeData;
import org.dei.perla.sys.registry.datastructure.BTreeNode;
import org.dei.perla.sys.registry.datastructure.FunctionalityProxyComponent;


import junit.framework.TestCase;

@SuppressWarnings("unchecked")
public class TestBtreeNode extends TestCase {

	private BTree<Integer> testTree = new BTree("temperature");
	private ArrayList<FunctionalityProxyComponent> fpcList = new ArrayList<FunctionalityProxyComponent>();
	
	protected void setUp() throws Exception {
		super.setUp();
	}

	public void test_add_remove_DataElement() {
		
		String attrType = "temperature";
		HashMap<String,Comparable> attrMap ;
		BTreeData  dataCur;
		FunctionalityProxyComponent fpcCur;
		FunctionalityProxyComponent fpcTemp =null;
		ArrayList<BTreeData> data = null;
		
		Integer[] fpc_atttr_val = {1,4,6,3,2,5,19,15,8,2,20,10,11,6,14,17,13,0,18,12,1,1,2,2};		
		
		for(int i=0; i<fpc_atttr_val.length;i++){
			
			attrMap = new HashMap<String,Comparable>();
			attrMap.put(attrType, fpc_atttr_val[i]);
			attrMap.put("pressure", fpc_atttr_val[i]*2);
			fpcCur  = new FunctionalityProxyComponent(attrMap);
			dataCur = new BTreeData(attrType);
			dataCur.setFPC(fpcCur);
			testTree.insert(dataCur);
			
			data = testTree.getData_In_Subtree(testTree.getRoot());
			
			
			assertEquals("Missing data: inserted ="+(i+1)+"; but size is: "+data.size(), data.size(), i+1 );
			
			fpcTemp = ((BTreeData)data.get(i)).getFPC();
			
		}
		
		//System.out.println(  testTree.toString());
		
		assertTrue("Attribute type not set", fpcTemp.getAttributesMap().containsKey("temperature"));
		
		BTreeNode cur_node = testTree.selectByValue(2);
		assertNotNull("And inerted element not found",cur_node);
		data = testTree.elementsAt(cur_node);
		
		assertEquals("unexpected data size", data.size(),4);
		
		int[] fpcid = new int[data.size()];
		Iterator idat = data.iterator();
		int i=0;
		while(idat.hasNext()){
			BTreeData btda =(BTreeData) idat.next();
			fpcid[i++] = btda.getFPC().getID(); 
		}
		
		int cur = data.size();
		for(int x=0; x<cur; x++){
			
			fpcCur = cur_node.getFPC(fpcid[x]);
			cur_node.deleteElement(fpcid[x] );
			
			assertNotNull ("Error in read by FpcId ", fpcCur );
			assertEquals("unmatched value of the element",0,(fpcCur.readAttributeval("temperature")).compareTo(2));
		}
		
		//System.out.println(  testTree.toString());
	
		
	}
	
	/*
	
	public void testGetAlldata() {
		fail("Not yet implemented");
	}

	public void testGetElement() {
		fail("Not yet implemented");
	}
	

	public void testRemoveElementInt() {
		//fail("Not yet implemented");
		
		
		
	}
	
	public void testRemoveElementComparable() {
		fail("Not yet implemented");
	}
*/

	
	

}
